package com.chenyi.shop.service.impl;

import java.util.*;

import com.chenyi.common.annotation.AdminDataScope;
import com.chenyi.common.annotation.ShopDataScope;
import com.chenyi.common.core.domain.entity.SysAdminRole;
import com.chenyi.common.utils.DateUtils;
import com.chenyi.common.utils.StringUtils;
import com.chenyi.common.utils.spring.SpringUtils;
import com.chenyi.shop.domain.SysShopRoleMenu;
import com.chenyi.shop.mapper.SysShopRoleMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.chenyi.shop.mapper.SysShopRoleMapper;
import com.chenyi.shop.domain.SysShopRole;
import com.chenyi.shop.service.ISysShopRoleService;

/**
 * 商户角色信息Service业务层处理
 * 
 * @author chenyi
 * @date 2020-10-24
 */
@Service
public class SysShopRoleServiceImpl implements ISysShopRoleService 
{
    @Autowired
    private SysShopRoleMapper sysShopRoleMapper;

    @Autowired
    private SysShopRoleMapper roleMapper;

    @Autowired
    private SysShopRoleMenuMapper roleMenuMapper;
    /**
     * 查询商户角色信息
     * 
     * @param roleId 商户角色信息ID
     * @return 商户角色信息
     */
    @Override
    public SysShopRole selectSysShopRoleById(Long roleId)
    {
        return sysShopRoleMapper.selectSysShopRoleById(roleId);
    }

    /**
     * 查询商户角色信息列表
     * 
     * @param sysShopRole 商户角色信息
     * @return 商户角色信息
     */
    @Override
    public List<SysShopRole> selectSysShopRoleList(SysShopRole sysShopRole)
    {
        return sysShopRoleMapper.selectSysShopRoleList(sysShopRole);
    }

    /**
     * 新增商户角色信息
     * 
     * @param shopRole 商户角色信息
     * @return 结果
     */
    @Override
    public int insertSysShopRole(SysShopRole shopRole)
    {
        shopRole.setCreateTime(DateUtils.getNowDate());
        sysShopRoleMapper.insertSysShopRole(shopRole);
        return insertShopRoleMenu(shopRole);
    }

    /**
     * 新增角色菜单信息
     *
     * @param shopRole 角色对象
     */
    public int insertShopRoleMenu(SysShopRole shopRole)
    {
        int rows = 1;
        // 新增用户与角色管理
        List<SysShopRoleMenu> list = new ArrayList<SysShopRoleMenu>();
        for (Long menuId : shopRole.getMenuIds())
        {
            SysShopRoleMenu rm = new SysShopRoleMenu();
            rm.setRoleId(shopRole.getRoleId());
            rm.setMenuId(menuId);
            list.add(rm);
        }
        if (list.size() > 0)
        {
            rows = roleMenuMapper.batchRoleMenu(list);
        }
        return rows;
    }

    /**
     * 修改商户角色信息
     * 
     * @param sysShopRole 商户角色信息
     * @return 结果
     */
    @Override
    public int updateSysShopRole(SysShopRole sysShopRole)
    {
        sysShopRole.setUpdateTime(DateUtils.getNowDate());
        sysShopRoleMapper.updateSysShopRole(sysShopRole);
        // 删除角色与菜单关联
        roleMenuMapper.deleteRoleMenuByRoleId(sysShopRole.getRoleId());
        return insertShopRoleMenu(sysShopRole);
    }

    /**
     * 批量删除商户角色信息
     * 
     * @param roleIds 需要删除的商户角色信息ID
     * @return 结果
     */
    @Override
    public int deleteSysShopRoleByIds(Long[] roleIds)
    {
        return sysShopRoleMapper.deleteSysShopRoleByIds(roleIds);
    }

    /**
     * 删除商户角色信息信息
     * 
     * @param roleId 商户角色信息ID
     * @return 结果
     */
    @Override
    public int deleteSysShopRoleById(Long roleId)
    {
        return sysShopRoleMapper.deleteSysShopRoleById(roleId);
    }

    @Override
    public Set<String> selectShopRolePermissionByShopId(Long shopId) {
        List<SysShopRole> perms = roleMapper.selectShopRolePermissionByShopId(shopId);
        Set<String> permsSet = new HashSet<>();
        for (SysShopRole perm : perms)
        {
            if (StringUtils.isNotNull(perm))
            {
                permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
            }
        }
        return permsSet;
    }
    /**
     * 查询所有角色
     *
     * @return 角色列表
     */
    @Override
    public List<SysShopRole>  selectShopRoleAll() {
        return selectSysShopRoleList(new SysShopRole());
    }
}
